package com.firefightingiot.firefightingiot.web.service;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.firefightingiot.firefightingiot.common.base.BaseService;
import com.firefightingiot.firefightingiot.common.utils.appkit.FirefightingiotKit;
import com.firefightingiot.firefightingiot.model.DeviceBusiness;
import com.firefightingiot.firefightingiot.support.model.RespEntity;
import com.firefightingiot.firefightingiot.support.mybatisplus.QueryWrapper;
import com.firefightingiot.firefightingiot.web.mapper.DeviceBusinessMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Yang
 */
@Service
@Slf4j
public class DeviceInfoService extends BaseService {

    @Resource
    private DeviceBusinessMapper deviceBusinessMapper;

    /**
     * 1.根据单位id查询出所有设备
     * 2.遍历集合
     * 3.遍历完放在JSONArray
     * 4. rest调用消防支队接口
     */
    public RespEntity DeviceInfo(JSONObject jsonObject) {
        String businessId = jsonObject.getStr("id");
        //根据单位id查询设备
        List<DeviceBusiness> deviceServiceList = deviceBusinessMapper.selectList(new QueryWrapper<DeviceBusiness>().eq("business_id", businessId));
        JSONObject jsonObject1 = new JSONObject();
        int size = deviceServiceList.size();
        int i = 0;
        JSONArray jsonArray = new JSONArray();
        for (DeviceBusiness business : deviceServiceList) {
            i = i + 1;
            jsonObject1 = new JSONObject();
            //电气火灾
            if(business.getDeviceClassify().equals("1")){
                String electricityMinValue = FirefightingiotKit.getStr("electricityMinValue","fire");
                String electricityMaxValue = FirefightingiotKit.getStr("electricityMaxValue","fire");
                jsonObject1.put("MinValue",electricityMinValue);
                jsonObject1.put("MaxValue ",electricityMaxValue);
                jsonObject1.put("DataType", "INSERT");
                jsonObject1.put("DevID", business.getNumber());
                jsonObject1.put("DevName", "电设备");
                jsonObject1.put("DevType", "01");
                jsonObject1.put("ComName", business.getBusinessName());
                jsonObject1.put("DevPos", business.getPosition());
                jsonObject1.put("ComID", "173022");//接待中心173022 //双语小学172817//
                jsonObject1.put("AlarmTel", "18369357036");
                jsonArray.add(jsonObject1);

           }//else

//               if(business.getDeviceClassify().equals("2")){//消防用水
//                if (StrUtil.isBlank(business.getWaterattr())){
//                    String electricityMinValue = FirefightingiotKit.getStr("waterPressureMinValue","fire");
//                    String electricityMaxValue = FirefightingiotKit.getStr("waterPressureMaxValue","fire");
//                    jsonObject1.put("MinValue",electricityMinValue);
//                    jsonObject1.put("MaxValue ",electricityMaxValue);
//                }else if (business.getWaterattr().equals("1")){//水压
//                    String electricityMinValue = FirefightingiotKit.getStr("waterPressureMinValue","fire");
//                    String electricityMaxValue = FirefightingiotKit.getStr("waterPressureMaxValue","fire");
//                    jsonObject1.put("MinValue",electricityMinValue);
//                    jsonObject1.put("MaxValue ",electricityMaxValue);
//                }else if (business.getWaterattr().equals("2")){//水位
//                    String electricityMinValue = FirefightingiotKit.getStr("waterLevelMinValue","fire");
//                    String electricityMaxValue = FirefightingiotKit.getStr("waterLevelMaxValue","fire");
//                    jsonObject1.put("MinValue",electricityMinValue);
//                    jsonObject1.put("MaxValue ",electricityMaxValue);
//                }
//                   jsonObject1.put("DataType", "INSERT");
//                   jsonObject1.put("DevID", business.getNumber());
//                   jsonObject1.put("DevName", business.getName());
//                   jsonObject1.put("DevType", "02");
//                   jsonObject1.put("ComName", business.getBusinessName());
//                   jsonObject1.put("DevPos", business.getPosition());
//                   jsonObject1.put("ComID", "173022");//接待中心173022 //双语小学172817//
//                   jsonObject1.put("AlarmTel", "18369357036");
//                   jsonArray.add(jsonObject1);
//            }

            if (size - i == 0 && !jsonArray.isEmpty()) {
                fireFightingiotTwo1(jsonArray, "Intelligentfire", "DeviceInfo", "jointFire");
                break;
            }
            if (i % 9 == 0) {
                fireFightingiotTwo1(jsonArray, "Intelligentfire", "DeviceInfo", "jointFire");
                jsonArray = new JSONArray();
            }
            }

        //{"DataType":"INSERT", "DevID":"GY1111", "DevName":"一楼配电室", "DevType":"01", "ComID":"123", "ComName":"XXXX公司", "DevPos":"", "MinValue":"","MaxValue":"100", "AlarmTel":""},

        return RespEntity.success(jsonArray);
    }
}

